/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.monitor.profile.model;

import com.hyts.monitor.profile.config.ProfilerConfig;
import lombok.*;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.monitor.profile.model
 * @author:LiBo/Alex
 * @create-date:2019-08-08 14:19
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
public class Filter {

    @Getter
    @Singular
    private Set<String> includePackages = new HashSet<>();

    @Getter
    @Singular
    private Set<String> excludePackages = ProfilerConfig.FilterConfig.DEFAULT_NOT_PROFILER_PACKAGE;

    @Getter
    @Singular
    private Set<String> excludeClassLoaders = new HashSet<>();

    @Getter
    @Singular
    private Set<String> excludeClassNames = new HashSet<>();

    /**
     * 赋值过滤不需要查看的package下面的类型
     */
    /*public Filter() {
        excludePackages.addAll(ProfilerConfig.FilterConfig.DEFAULT_NOT_PROFILER_PACKAGE);
    }*/
    /**
     * 是否属于过滤
     * @param className
     * @return
     */
    public boolean isExclude(String className){
        Objects.requireNonNull(className);
        //校验exclude类中是否不包含
        return excludeClassNames.parallelStream().anyMatch(className::equals)
               ||
               includePackages.parallelStream().noneMatch(className::startsWith)
               ||
               excludePackages.parallelStream().anyMatch(className::startsWith);

    }
    /**
     * 是否属于包含
     * @param className
     * @return
     */
    public boolean isInclude(String className){
        Objects.requireNonNull(className);
        //校验include类中是否包含
        return !isExclude(className);
    }

    /**
     *
     * @param classLoader
     * @return
     */
    public boolean isExcludeClassLoader(String classLoader){
        return excludeClassLoaders.parallelStream().anyMatch(classLoader::equals);
    }
}
